package com.joker;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * @Author JokerKS
 * @Date 2021/12/31 09:52
 * @Version 1.0
 */
public class TestTX {
	public static void main(String[] args) {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.flushDB();
		Transaction multi = jedis.multi();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", "joker");
		jsonObject.put("age", "12");

		String s = jsonObject.toJSONString();

		try {
			multi.set("user1", s);
			multi.set("user2", s);
			int i = 1 / 0; //模拟异常
			multi.exec();//执行事务
		} catch (Exception e) {
			multi.discard();//放弃事务
			e.printStackTrace();
		} finally {
			System.out.println(jedis.get("user1"));
			System.out.println(jedis.get("user2"));
			multi.close();//关闭连接
		}
	}
}
